package com.hyg.module_report.adapter;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hyg.lib_base.mainUtils.Utils;
import com.hyg.module_report.R;
import com.hyg.module_report.model.HealthCareData;

import java.util.ArrayList;


/*  养生建议页面的适配器  */
public class HealthAdviceAdapter extends RecyclerView.Adapter<HealthAdviceAdapter.ViewHolder> {
    private Context context;
    private ArrayList<HealthCareData> dataList;
    private OnItemClickLitener mOnItemClickLitener;

    public HealthAdviceAdapter(Context activity, ArrayList<HealthCareData> dataList) {
        this.context = activity.getApplicationContext();
        this.dataList = dataList;
    }

    public void addItem(HealthCareData content, int position) {
        dataList.add(position, content);
        notifyItemInserted(position); //Attention!
    }

    public void removeItem(HealthCareData model) {
        int position = dataList.indexOf(model);
        dataList.remove(position);
        notifyItemRemoved(position);//Attention!
    }
    public void removeData(int position) {
        dataList.remove(position);
        notifyItemRemoved(position);
    }

    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_health_advice_detail,
                parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.bindView(dataList.get(position), position);
    }

    @Override
    public int getItemCount() {
        if(dataList==null) {
            return 0;
        }
        return dataList.size();
    }
    @Override
    public int getItemViewType(int position) {
        // 适合在添加上拉下拉加载其他页面时使用,同时防止 RecyclerView在快速滑动时，造成数据错乱
        return position;
        // 本方法在 onCreateViewHolder 之前调用，所以设置 多种布局的类型
    }


    class ViewHolder extends RecyclerView.ViewHolder  {
        private ImageView detail_image;
        private TextView detail_title;
        private TextView Subtitle1,ShortEssay1,Subtitle2,ShortEssay2,Subtitle3,ShortEssay3,Subtitle4,ShortEssay4,Subtitle5,ShortEssay5;

        public ViewHolder(View itemView) {
            super(itemView);
            detail_image = (ImageView)itemView.findViewById(R.id.detail_image);
            detail_title = (TextView) itemView.findViewById(R.id.detail_title);
            Subtitle1 = (TextView)itemView.findViewById(R.id.Subtitle1);
            Subtitle2 = (TextView)itemView.findViewById(R.id.Subtitle2);
            Subtitle3 = (TextView)itemView.findViewById(R.id.Subtitle3);
            Subtitle4 = (TextView)itemView.findViewById(R.id.Subtitle4);
            ShortEssay1 = (TextView)itemView.findViewById(R.id.ShortEssay1);
            ShortEssay2 = (TextView)itemView.findViewById(R.id.ShortEssay2);
            ShortEssay3 = (TextView)itemView.findViewById(R.id.ShortEssay3);
            ShortEssay4 = (TextView)itemView.findViewById(R.id.ShortEssay4);
        }

        public void bindView(final HealthCareData AdviceData, final int pos){
//            HealthCareData AdviceData = HealthCareData;
            if(!Utils.isEmpty(AdviceData.getCareTitle())) {
                detail_title.setText(AdviceData.getCareType() + " - " + AdviceData.getCareTitle());
            }else{
                detail_title.setText(AdviceData.getCareType());
            }
            Glide.with(context).load(AdviceData.getCareImageUrl()).into(detail_image);

            if(AdviceData.getCareType().equals("运动养生")){
//                Subtitle1.setText(getInsideString(AdviceData.getAdviceDescribe(),"【","】"));
//                ShortEssay1.setText("\u3000\u3000" +AdviceData.getAdviceDescribe().trim().substring(AdviceData.getCareContent().indexOf("】")+1));
                Subtitle1.setVisibility(View.GONE);
                ShortEssay1.setVisibility(View.GONE);
                Subtitle2.setText(getInsideString(AdviceData.getAdviceOperation(),"【","】"));
                ShortEssay2.setText("\u3000\u3000" +AdviceData.getAdviceOperation().trim().substring(AdviceData.getAdviceOperation().indexOf("】")+1));
                Subtitle3.setText(getInsideString(AdviceData.getAdviceRole(),"【","】"));
                ShortEssay3.setText("\u3000\u3000" +AdviceData.getAdviceRole().trim().substring(AdviceData.getAdviceRole().indexOf("】")+1));
                Subtitle4.setText(getInsideString(AdviceData.getAdviceTaboo(),"【","】"));
                ShortEssay4.setText("\u3000\u3000" +AdviceData.getAdviceTaboo().trim().substring(AdviceData.getAdviceTaboo().indexOf("】")+1));
                Subtitle2.setVisibility(View.VISIBLE);
                ShortEssay2.setVisibility(View.VISIBLE);
                Subtitle3.setVisibility(View.VISIBLE);
                ShortEssay3.setVisibility(View.VISIBLE);
                Subtitle4.setVisibility(View.VISIBLE);
                ShortEssay4.setVisibility(View.VISIBLE);
            }else if(AdviceData.getCareType().equals("膳食养生") || AdviceData.getCareType().equals("药浴养生") || AdviceData.getCareType().equals("茶饮养生")){

                if(!Utils.isEmpty( AdviceData.getAdviceDescribe() )) {
                    Subtitle1.setText("【原料】");
                    ShortEssay1.setText("\u3000\u3000" +AdviceData.getAdviceDescribe().trim());
                }else{
                    Subtitle1.setVisibility(View.GONE);
                    ShortEssay1.setVisibility(View.GONE);
                }
                if(!Utils.isEmpty( AdviceData.getAdviceOperation() )) {
                    Subtitle2.setText("【做法】");
                    ShortEssay2.setText("\u3000\u3000" +AdviceData.getAdviceOperation().trim());
                }else{
                    Subtitle2.setVisibility(View.GONE);
                    ShortEssay2.setVisibility(View.GONE);
                }
                if(!Utils.isEmpty( AdviceData.getAdviceRole() )) {
                    Subtitle3.setText("【功效】");
                    ShortEssay3.setText("\u3000\u3000" +AdviceData.getAdviceRole().trim());
                }else{
                    Subtitle3.setVisibility(View.GONE);
                    ShortEssay3.setVisibility(View.GONE);
                }
                if(!Utils.isEmpty( AdviceData.getAdviceTaboo() )) {
                    Subtitle4.setText("【禁忌】");
                    ShortEssay4.setText("\u3000\u3000" +AdviceData.getAdviceTaboo().trim());
                }else{
                    Subtitle4.setVisibility(View.GONE);
                    ShortEssay4.setVisibility(View.GONE);
                }

            }else if(AdviceData.getCareType().equals("艾灸养生")){
                if(!Utils.isEmpty( AdviceData.getAdviceTaboo() )){
                    Subtitle1.setText("【取穴位置】");
                    ShortEssay1.setText("\u3000\u3000" +AdviceData.getAdviceTaboo() .trim());
                }else{
                    Subtitle1.setVisibility(View.GONE);
                    ShortEssay1.setVisibility(View.GONE);
                }
                if(!Utils.isEmpty( AdviceData.getAdviceDescribe() )) {
                    Subtitle2.setText("【穴位描述】");
                    ShortEssay2.setText("\u3000\u3000" +AdviceData.getAdviceDescribe().trim());
                }else{
                    Subtitle2.setVisibility(View.GONE);
                    ShortEssay2.setVisibility(View.GONE);
                }
                if(!Utils.isEmpty( AdviceData.getAdviceOperation() )) {
                    Subtitle3.setText("【主治操作】");
                    ShortEssay3.setText("\u3000\u3000" +AdviceData.getAdviceOperation().trim());
                }else{
                    Subtitle3.setVisibility(View.GONE);
                    ShortEssay3.setVisibility(View.GONE);
                }
                if(!Utils.isEmpty( AdviceData.getAdviceRole() )) {
                    Subtitle4.setText("【作用功效】");
                    ShortEssay4.setText("\u3000\u3000" +AdviceData.getAdviceRole().trim());
                }else{
                    Subtitle4.setVisibility(View.GONE);
                    ShortEssay4.setVisibility(View.GONE);
                }
            }else if(AdviceData.getCareType().equals("楔子养生") || AdviceData.getCareType().equals("应季养生")){
                Subtitle1.setVisibility(View.VISIBLE);
                ShortEssay1.setVisibility(View.VISIBLE);
                Subtitle1.setText("");
                ShortEssay1.setText("\u3000\u3000" +AdviceData.getCareContent().trim());
                Subtitle2.setVisibility(View.GONE);
                ShortEssay2.setVisibility(View.GONE);
                Subtitle3.setVisibility(View.GONE);
                ShortEssay3.setVisibility(View.GONE);
                Subtitle4.setVisibility(View.GONE);
                ShortEssay4.setVisibility(View.GONE);
            }else{
                Subtitle1.setVisibility(View.VISIBLE);
                ShortEssay1.setVisibility(View.VISIBLE);
                Subtitle2.setVisibility(View.VISIBLE);
                ShortEssay2.setVisibility(View.VISIBLE);
                Subtitle3.setVisibility(View.VISIBLE);
                ShortEssay3.setVisibility(View.VISIBLE);
                Subtitle4.setVisibility(View.VISIBLE);
                ShortEssay4.setVisibility(View.VISIBLE);
            }
        }
        // 截取两个指定字符串之间的内容
        public String getInsideString(String str, String strStart, String strEnd) {
            if (str.indexOf(strStart) < 0) {
                return "";
            }
            if (str.indexOf(strEnd) < 0) {
                return "";
            }
            return str.substring(str.indexOf(strStart) + strStart.length()-1, str.indexOf(strEnd)+1);
        }
    }
}